﻿using System.Windows.Media.Converters;
using SubhadraSolutions.Sharp.Utils.Algorithms;
using SubhadraSolutions.Sharp.Utils.Collections.ObjectModel;

namespace SubhadraSolutions.Sharp.Utils.TestApplication.Wpf
{
    using System;
    using System.Collections.Generic;
    using System.Threading;
    
    using SubhadraSolutions.Sharp.Utils.Collections.Concurrent;
    using SubhadraSolutions.Sharp.Utils.Collections.Generic;
    using SubhadraSolutions.Sharp.Utils.Tests.DummyClasses;
    using SubhadraSolutions.Sharp.Utils.Windows;

    public static class ListNotifyCollectionChangedAdapterTestsLauncher
    {
        public static void Launch()
        {
            ThreadPool.QueueUserWorkItem(delegate { PerformDispatcherTest(); });
        }

        public static void LaunchWithDifferentCollectionsWithSharedObjects()
        {
            var list = new List<Employee1>();
            for (int i = 0; i < 100; i++)
            {
                var e = Employee1.BuildDummyEmployee();
                list.Add(e);
            }


            for (int i = 0; i < 5; i++)
            {
                var listToBind =
                    new DispatcherAwareObservableCollection<Employee1>(ThreadBasedWindowsDispatcherProvider.Instance);
                foreach (var v in list)
                {
                    listToBind.Add(v);
                }

                var thread = new Thread(delegate()
                {
                    var window = CreateWindow(listToBind);
                    window.ShowDialog();
                });
                thread.SetApartmentState(ApartmentState.STA);
                thread.Start();
                //ThreadPool.QueueUserWorkItem(action);
            }

            Thread.Sleep(3000);


            ThreadPool.QueueUserWorkItem(delegate
            {
                var random = new Random();
                while (true)
                {
                    var index = random.Next(list.Count);
                    var e = list[index];
                    e.Id = random.Next();
                    e.FirstName = RandomHelper.BuildRandomString(10);
                    e.LastName = RandomHelper.BuildRandomString(10);
                    Thread.Sleep(50);
                }
            });
        }

        /// <summary>
        /// Performs the dispatcher test.
        /// </summary>
        private static void PerformDispatcherTest()
        {
            IList<Employee1> list = new List<Employee1>();
            list = new SafeListDecorator<Employee1>(list);
            list = new DispatcherAwareListNotifyCollectionChangedAdapter<Employee1>(list, ThreadBasedWindowsDispatcherProvider.Instance) { new Employee1 { Id = 0, FirstName = "SubhadraSolutionsmanya", LastName = "Oruganti", DateOfBirth = new DateTime(1979, 5, 1), DateOfJoin = new DateTime(2013, 10, 24), EmploymentType = EmploymentType.FullTime } };
            //var list = new DispatcherAwareObservableCollection<Employee1>(ThreadBasedWindowsDispatcherProvider.Instance);
            //var list = new ObservableCollection<Employee>();

            for (int i = 0; i < 5; i++)
            {
                var thread = new Thread(delegate()
                    {
                        var window = CreateWindow(list);
                        window.ShowDialog();
                    });
                thread.SetApartmentState(ApartmentState.STA);
                thread.Start();
                //ThreadPool.QueueUserWorkItem(action);
            }

            Thread.Sleep(3000);
            for (int i = 0; i < 1; i++)
            {
                ThreadPool.QueueUserWorkItem(delegate
                    {
                        while (true)
                        {
                            var emp = new Employee1 { Id = (int)GeneralHelper.Identity, FirstName = "SubhadraSolutionsmanya", LastName = "Oruganti", DateOfBirth = new DateTime(1979, 5, 1), DateOfJoin = new DateTime(2013, 10, 24), EmploymentType = EmploymentType.FullTime };
                            list.Add(emp);
                            Thread.Sleep(500);
                            //list.RemoveAt(0);
                        }
                    });
            }

            ThreadPool.QueueUserWorkItem(delegate
                    {
                        while (true)
                        {
                            var enumerator = list.GetEnumerator();
                            enumerator.MoveNext();
                            Thread.Sleep(5000);
                            enumerator.Dispose();
                            Thread.Sleep(5000);
                        }
                    });
        }

        private static DataGridWindow CreateWindow(IList<Employee1> dataSource)
        {
            var window = new DataGridWindow(dataSource);
            return window;
        }

    }
}
